'''
    定时任务部署文件

    描述
    ========
    此处放置着各子系统所需要执行的各类型定时任务
    具体包括如下任务:
        -`inital_fund_list`: 每隔5分钟筛选一下基金列表
'''

import multiprocessing
from apscheduler.schedulers.background import BackgroundScheduler
from django_apscheduler.jobstores import DjangoJobStore, register_events, register_job
from BusinessSubSystem.algorithmFunction import fund_selection_rule, bond_fund_selection_rules

#开启定时工作
try:
    # 实例化调度器
    scheduler = BackgroundScheduler()
    # 调度器使用DjangoJobStore()
    scheduler.add_jobstore(DjangoJobStore(), "default")

    # 设置定时任务，选择方式为interval，时间间隔为10s

    @register_job(scheduler,"interval", minutes=5,id="create_selected_fund_list",replace_existing=True)
    def my_business_should_do_time_job():
        inital_fund_list()

    register_events(scheduler)
    scheduler.start()
except Exception as e:
    print(e)
    # 有错误就停止定时器
    scheduler.shutdown()


def inital_fund_list():
    task_risk_fund_one = multiprocessing.Process(target=fund_selection_rule('1'))
    task_risk_fund_two = multiprocessing.Process(target=fund_selection_rule('2'))
    task_risk_fund_three = multiprocessing.Process(target=fund_selection_rule('3'))
    task_risk_fund_four = multiprocessing.Process(target=fund_selection_rule('4'))
    task_bond_fund_one = multiprocessing.Process(target=bond_fund_selection_rules('1'))
    task_bond_fund_two = multiprocessing.Process(target=bond_fund_selection_rules('2'))
    task_bond_fund_three = multiprocessing.Process(target=bond_fund_selection_rules('3'))
    task_bond_fund_four = multiprocessing.Process(target=bond_fund_selection_rules('4'))
    task_risk_fund_one.start()
    task_risk_fund_two.start()
    task_risk_fund_three.start()
    task_risk_fund_four.start()
    task_bond_fund_one.start()
    task_bond_fund_two.start()
    task_bond_fund_three.start()
    task_bond_fund_four.start()

